# Generating a table to display results from the glms

# Season 1
summary(model4)
summary(model5)
summary(model6)
summary(model7)

# Season 2
summary(model.1)
summary(model.4)
summary(model.5)
summary(model.7)

# Collect results for each model
results <- list()

# SEason 1
models <- list(model4, model5, model6, model7)
formulas <- c("Crown extent ~ P50 + osmoticpot + HV", "Foliage ~ LMA + height + PPE", "Dead leaves ~ P50 + MOE + height","Discolouration ~ moe + osmoticpot + HV")
for (i in 1:length(models)) {
  model <- models[[i]]
  
  # McFadden's R^2
  mcfadden_r2 <- 1 - (model$deviance / model$null.deviance)
  
  # AIC
  aic_value <- AIC(model)
  
  # Deviance explained
  deviance_explained <- (model$null.deviance - model$deviance) / model$null.deviance
  
  # Number of observations
  n_obs <- nrow(data)
  
  # Number of predictors
  n_predictors <- length(coef(model)) - 1
  
  # Store in results list
  results[[i]] <- data.frame(
    Formula = formulas[i],
    AIC = aic_value,
    McFadden_R2 = mcfadden_r2,
    Deviance_Explained = deviance_explained,
    N_Obs = n_obs,
    N_Predictors = n_predictors
  )
}

# Combine all results into one data frame
results_df <- do.call(rbind, results)

# View the final table
print(results_df)
# Format table with knitr's kable
# install.packages("knitr")
# install.packages("officer")

colnames(results_df)

# Create a gt table
# Create and format the table with manual row striping
# Create and format the table
# Install necessary packages if not already installed
# install.packages("knitr")
# install.packages("kableExtra")

# Load the libraries

# Create a basic table with kable()
results_kable <- kable(results_df, format = "html", caption = "GLM Results", align = "c")

# Add extra styling with kableExtra
styled_table <- results_kable %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F) %>%
  column_spec(1, width = "25em")  # Adjust column width for 'Formula'

# Print the table to view it
styled_table

# Season 2
models <- list(model.1, model.4, model.5, model.7)
formulas <- c("Crown extent ~ HV + height + PPE", "Foliage ~ P50 + HV + height", "Dead leaves ~ osmoticpot + HV + height","Discoloration ~ P50 + height + PPE")

for (i in 1:length(models)) {
  model <- models[[i]]
  
  # McFadden's R^2
  mcfadden_r2 <- 1 - (model$deviance / model$null.deviance)
  
  # AIC
  aic_value <- AIC(model)
  
  # Deviance explained
  deviance_explained <- (model$null.deviance - model$deviance) / model$null.deviance
  
  # Number of observations
  n_obs <- nrow(data)
  
  # Number of predictors
  n_predictors <- length(coef(model)) - 1
  
  # Store in results list
  results[[i]] <- data.frame(
    Formula = formulas[i],
    AIC = aic_value,
    McFadden_R2 = mcfadden_r2,
    Deviance_Explained = deviance_explained,
    N_Obs = n_obs,
    N_Predictors = n_predictors
  )
}

# Combine all results into one data frame
results_df <- do.call(rbind, results)

# View the final table
print(results_df)
# Format table with knitr's kable
# install.packages("knitr")
# install.packages("officer")

colnames(results_df)

# Create a gt table
# Create and format the table with manual row striping
# Create and format the table
# Install necessary packages if not already installed
# install.packages("knitr")
# install.packages("kableExtra")

# Load the libraries

# Create a basic table with kable()
results_kable <- kable(results_df, format = "html", caption = "GLM Results", align = "c")

# Add extra styling with kableExtra
styled_table <- results_kable %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F) %>%
  column_spec(1, width = "25em")  # Adjust column width for 'Formula'

# Print the table to view it
styled_table